package com.af.community.smart.business

import android.content.Intent
import android.os.Bundle
import com.af.community.smart.base.*
import com.af.community.smart.utils.OSSHelper
import com.af.community.smart.utils.bindLifeCycle
import com.af.community.smart.utils.scaleImage
import com.af.community.smart.utils.subscribeByNoError
import com.af.community.smart.viewmodel.InfoViewModel
import com.lxj.matisse.Matisse
import com.lxj.matisse.MimeType
import kotlinx.android.synthetic.main.activity_info.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class InfoActivity : BaseActivity() {

    private val REQUEST_CODE_CHOOSE = 30

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_info)
        initTitle(title = "修改信息")
    }

    override fun getViewModel() = getViewModel<InfoViewModel>()

    override fun initTitle(title: String, name: String) {
        super.initTitle(title, name)
        info_img.loadCircleImage(getString("avatar").scaleImage())

        info_pwd.oneClick { startActivity<PasswordActivity>() }
        info_head.oneClick {
            Matisse.from(baseContext)
                //显示文件类型
                .choose(MimeType.ofImage())
                //主题色
                .theme(R.style.Matisse_Blue)
                //是否显示拍摄按钮，默认不显示
                .capture(true)
                //是否显示原图，默认显示
                .originalEnable(false)
                //设置原图选中和取消的监听
                .setOnCheckedListener {  }
                //默认最大选9张，设置为1就是单选
                .maxSelectable(1)
                //设置选中图片的监听器
                .setOnSelectedListener { _, _ ->  }
                //开启裁剪，默认不开启
                .isCrop(true)
                //请求码
                .forResult(REQUEST_CODE_CHOOSE)
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
            val cropPath = Matisse.obtainCropResult(data)

            OSSHelper.manager.uploadImage(
                path = cropPath,
                onBefore = { showLoading("正在上传中...") },
                onAfter = { dismissLoading() },
                onSuccess = {
                    val mUrl = it.responseHeader["url"].orEmpty()
                    getViewModel().getUpdateData(
                        "userId" to getString("userId"),
                        "avatar" to mUrl
                    )
                        .bindLifeCycle(getLifecycleOwner())
                        .subscribeByNoError(
                            event = getViewModel(),
                            onSuccess = {
                                putString("avatar", mUrl)
                                info_img.loadCircleImage(mUrl.scaleImage())
                                toast("图片上传成功！")
                            }
                        )
                },
                OnError = { toast("网络异常，请稍候再试！") }
            )
        }
    }

}
